var essayID = window.location.search;
var essayid = essayID.split("=");

var wrapper = new Vue({
		el:"#wrapper",
		data:{
			result:"",
			marked:"",
			htmled:"",
			title: '',
			logon: true,
			hot: '',
			user: '',
			email: '未登录',
			word: '',
			uname: '',
			uemail: '',
			ucontent: '',
		},
		methods:{
			getEssay:function(){
				var that = this;
				// var testView = editormd.markdownToHTML("test-markdown-view", {
				// 	markdown : that.marked, // Also, you can dynamic set Markdown text
				// 	htmlDecode : true,  // Enable / disable HTML tag encode.
				// 	htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
				//  });
				if(essayID==null){
					alert("文章出错！");
					window.location.href="home.html";
				}
				// this.email = sessionStorage.getItem("email");
				// if(this.email!=null){
				// 	this.turnOn = false;
				// }
				axios.get(base_ure+'/essay/EID?EID='+essayid[1]).then(function(res){
				    if(res.data.code == "200")
				    {
				        that.result = res;
						// console.log(res);
						that. marked = res.data.data;
						that. title = res.data.data.title;
						var testView = editormd.markdownToHTML("test-markdown-view", {
							markdown : that. marked.content, // Also, you can dynamic set Markdown text
							htmlDecode : true,  // Enable / disable HTML tag encode.
							htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
						 });
				    }
					else{
						alert(res.data.msg);
						window.location.href="home.html";
					}
				},function(err){
				    alert("加入出错");
				})
				
			},
			getEmail:function(){
				var emil1 = sessionStorage.getItem("email");
				if(emil1 != null){
					this.email = emil1;
					this.logon = !this.logon;
				}
			},
			getHot:function(){
				var that = this;
				axios.get(base_ure+'/essay/hot').then(function(res){
				    if(res.data.code == "200")
				    {
				        that.hot = res.data.data;
				    }
					else{
						alert(res.data.msg);
					}
				},function(err){
				    alert("网络出错！");
				})
			},
			getUser:function(){
				var that = this;
				axios.get(base_ure+'/user/getUser').then(function(res){
				    if(res.data.code == "200")
				    {
				        that.user = res.data.data;
				    }
					else{
						alert(res.data.msg);
					}
				},function(err){
				    alert("网络出错！");
				})
			},
			logout:function(){
				sessionStorage.removeItem("email");
				sessionStorage.removeItem("uuid");
				window.location.reload();
			},
			getWord:function(){
				var that = this;
				axios.get(base_ure+'/comment/getW?eid='+essayid[1]).then(function(res){
				    if(res.data.code == "200")
				    {
				        that.word = res.data.data;
				    }
					else{
						alert(res.data.msg);
					}
				},function(err){
				    alert("网络出错！");
				})
			},
			addWord:function(){
				var that = this;
				axios.post(base_ure+'/comment/say', {
				    email: that.uemail,
					username: that.uname,
					content: that.ucontent,
					eid: essayid[1],
				})
				.then(function (response) {
					 if(response.data.code == "200"){
						 that.uemail = '';
						 that.uname = '';
						 that.ucontent = '';
						 that.getWord();
						 
					 }else{
						 alert("加入失败，请重试！");
						 return false;
					 }
				})
				.catch(function (error) {
				 console.log(error);
				 alert("出错了，请重试！");
				});
			},
			deleteE:function(){
				var that = this;
				if(that. marked.email != sessionStorage.getItem("email")){
					alert("出错了，你不可以删除其他人的博文！");
					return false;
				}
				// alert(sessionStorage.getItem("email") + " " + sessionStorage.getItem("uuid") + " " +that. marked.eid);
				axios.post(base_ure+'/essay/delete', {
				    email: sessionStorage.getItem("email"),
					uuid: sessionStorage.getItem("uuid"),
					eid: that. marked.eid,
				})
				.then(function (response) {
					 console.log(response);
					 if(response.data.code == "200"){
						alert("删除成功！");
						window.location.href = "home.html";
					 }else{
						 alert("失败，请重试！");
						 return false;
					 }
				})
				.catch(function (error) {
				 console.log(error);
				 alert("出错了，请重试！");
				});
			},
			//判断是不是自己的文章，然后跳转一个新页面带上eid
			edit:function(){
				var that = this;
				if(that. marked.email != sessionStorage.getItem("email")){
					alert("出错了，你不可以编辑其他人的博文！");
					return false;
				}
				window.location.href = "edit.html?EID="+essayid[1];
			},
		},
		mounted() {
			this.getEmail();
			this.getEssay();
			this.getHot();
			this.getUser();
			this.getWord();
		},
})